package com.system.controller;

import com.system.model.SysPermission;
import com.system.model.SysRole;
import com.system.model.SysRolePermission;
import com.system.service.SysPermissionService;
import com.system.service.SysRolePermissionService;
import com.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/sysPermissionController")
public class SysPermissionController {

    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    /**
     * 全查权限列表
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysPermission
     * @return
     */
    @RequestMapping("/sysPermissionList")
    public String sysPermissionList(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission){
        //接收数据
        //执行业务
        //跳转页面
        List<SysPermission> sysPermissionList = sysPermissionService.queryAllSysPermission();
        session.setAttribute("sysPermissionList",sysPermissionList);
        return "view/system/sysPermission/sysPermissionList";
    }

    @RequestMapping("/sysPermissionAddUI")
    public String sysPermissionAddUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission) {
        //1.接收数据
        //3.调用业务
        //准备父级节点数据
        List<SysPermission> sysPermissionList_db = sysPermissionService.queryAllSysPermission();
        model.addAttribute("sysPermissionList_db", sysPermissionList_db);
        //2.跳转页面
        return "view/system/sysPermission/sysPermissionAdd";
    }


    /**
     * 添加权限信息
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysPermission
     * @return
     */
    @RequestMapping("/sysPermissionAdd")
    public String sysPermissionAdd(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission, String pIdSTR){
        //接收数据
        //执行业务
        int pId = Integer.parseInt(pIdSTR);
        sysPermission.setpId(pId);
        //跳转页面
        int i = sysPermissionService.sysPermissionAdd(sysPermission);
        if(i!=0){
            List<SysPermission> sysPermissionList = sysPermissionService.queryAllSysPermission();
            session.setAttribute("sysPermissionList",sysPermissionList);
            return "view/system/sysPermission/sysPermissionList";
        }
        model.addAttribute("message","添加权限失败！");
        return "view/system/sysPermission/sysPermissionAdd";
    }

    /**
     * 权限信息修改UI（传递信息）
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysPermission
     * @param sysPermissionIdStr
     * @return
     */
    @RequestMapping("/sysPermissionUpdateUI")
    public String sysPermissionUpdateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission, String sysPermissionIdStr){
        //接收数据
        //执行业务
        //跳转页面
        SysPermission sysPermission_db= sysPermissionService.sysPermissionUpdateUI(sysPermissionIdStr);
        if(sysPermission_db!=null){
            model.addAttribute("sysPermission_db",sysPermission_db);
            List<SysPermission> sysPermissionList_db = sysPermissionService.queryAllSysPermission();
            model.addAttribute("sysPermissionList_db", sysPermissionList_db);
            return "view/system/sysPermission/sysPermissionUpdate";
        }
        return "view/system/sysPermission/sysPermissionList";
    }

    /**
     * 修改角色信息
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysPermission
     * @return
     */
    @RequestMapping("/sysPermissionUpdate")
    public String sysPermissionUpdate(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission, String pIdSTR){
        //接收数据
        //执行业务
        int pId = Integer.parseInt(pIdSTR);
        //3.调用业务
        sysPermission.setpId(pId);
        //跳转页面
        int i= sysPermissionService.sysPermissionUpdate(sysPermission);
        if(i!=0){
            List<SysPermission> sysPermissionList = sysPermissionService.queryAllSysPermission();
            session.setAttribute("sysPermissionList",sysPermissionList);
            return "view/system/sysPermission/sysPermissionList";
        }
        model.addAttribute("message","修改权限信息失败！");
        return "view/system/sysPermission/sysPermissionUpdate";
    }

    /**
     * 删除角色信息
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysPermission
     * @param sysPermissionIdStr
     * @return
     */
    @RequestMapping("/sysPermissionDelete")
    public String sysPermissionDelete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission, String sysPermissionIdStr){
        //接收数据
        //执行业务
        //跳转页面
        int i= sysPermissionService.sysPermissionDelete(sysPermissionIdStr);
        if(i!=0){
            List<SysPermission> sysPermissionList = sysPermissionService.queryAllSysPermission();
            session.setAttribute("sysPermissionList",sysPermissionList);
            return "view/system/sysPermission/sysPermissionList";
        }
        model.addAttribute("message","删除权限信息失败！");
        return "view/system/sysPermission/sysPermissionList";
    }


    @RequestMapping("/FPPermissionUI")
    public String FPPermissionUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission, String sysPermissionIdStr, String sysRoleIdStr){
        //1.接收数据
        //3.调用业务

        //a.角色数据
        SysRole sysRole_db = sysRoleService.sysRoleUpdateUI(sysRoleIdStr);
        model.addAttribute("sysRole_db", sysRole_db);

        //b.分配权限数据
        List<SysPermission> sysPermissionList_db = sysPermissionService.queryAllSysPermission();
        model.addAttribute("sysPermissionList_db", sysPermissionList_db);

//        SysUserRole sysUserRole_db=sysUserRoleService.querySysUserRoleByUserId(sysUserId);
//        model.addAttribute("sysUserRole_db", sysUserRole_db);
        //2.跳转页面
        List<SysPermission> sysPermissionList_db_xz=sysPermissionService.queryXZSysPermissionByRoleId(Integer.parseInt(sysRoleIdStr));
        List<Integer> list= new ArrayList<>();
        for (SysPermission permission : sysPermissionList_db_xz) {
            list.add(permission.getPermissionId());
        }
        request.setAttribute("permissionIds",list);

        return "view/system/sysPermission/FPPermissionUI";
    }

    @RequestMapping("/FPPermission")
    public String FPPermission(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRolePermission sysRolePermission , SysPermission sysPermission, String[] sysPermissionIdStrs, String sysRoleIdStr){
        //1.接收数据
        int sysRoleId = Integer.parseInt(sysRoleIdStr);

        //3.调用业务
        //-->中间表增加数据，先判断数据库有没有
        int m = sysRolePermissionService.deleteSysRolePermissionByRoleId(sysRoleId);
        //增加
        for (String sysPermissionIdStr : sysPermissionIdStrs) {
            int sysPermissionId = Integer.parseInt(sysPermissionIdStr);
            sysRolePermission.setRoleId(sysRoleId);
            sysRolePermission.setPermissionId(sysPermissionId);
            int n = sysRolePermissionService.addSysRolePermissionBySysRolePermission(sysRolePermission);
        }

        //2.跳转页面
        //a.角色数据
        SysRole sysRole_db = sysRoleService.sysRoleUpdateUI(sysRoleIdStr);
        model.addAttribute("sysRole_db", sysRole_db);

        //b.分配权限数据
        List<SysPermission> sysPermissionList_db = sysPermissionService.queryAllSysPermission();
        model.addAttribute("sysPermissionList_db", sysPermissionList_db);

//        选中数据
        List<SysPermission> sysPermissionList_db_xz=sysPermissionService.queryXZSysPermissionByRoleId(sysRoleId);
        List<Integer> list= new ArrayList<>();
        for (SysPermission permission : sysPermissionList_db_xz) {
            list.add(permission.getPermissionId());
        }
        request.setAttribute("permissionIds",list);

        return "view/system/sysPermission/FPPermissionUI";
    }
}
